const https = require("https");

function Get(url, option) {
    return new Promise((resolve, reject) => {
        https.get(url, (res) => {
            let data = "";
            res.on('data', (chunk) => {
                data += chunk;
            });
            res.on("end", () => {
                resolve(data);
            })
        }).on("error", (e) => {
            reject(e);
        });
    })
}

async function start(){
    let song_name = "My Love (Radio Edit)";
    let singer = "Westlife";
    let album = "My Love";
    let id = null;

    // 获取歌曲id
    const url1 = 'https://music.163.com/api/search/get?s=' + song_name + '&type=1&offset=0&limit=30';
    const options1 = { method: 'GET' }
    data = await Get(url1, options1);
    const jsonResponse = JSON.parse(data);
    for(let i = 0; i < jsonResponse.result.songs.length; i++){        
        if((jsonResponse.result.songs[i].name === song_name) && (jsonResponse.result.songs[i].artists[0].name === singer) && (jsonResponse.result.songs[i].album.name === album)){
            id = jsonResponse.result.songs[i].id;
        }
    }

    // 根据歌曲id获取歌词
    const url2 = "https://music.163.com/api/song/lyric?os=pc&id=" + id + "&lv=-1&tv=-1";
    const options2 = { method: 'GET' }
    data = await Get(url2, options2);
    console.log(JSON.parse(data).lrc.lyric);
}

start();
